﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BrowserGameFramework.Controls.ControlCommon;
using BrowserGameFramework.Common.Exceptions;


namespace BrowserGameFramework.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MapControl runat=server></{0}:MapControl>")]
    public class MapControl : CompositeControl
    {
        #region constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="MapControl"/> class.
        /// </summary>
        [Obsolete("This Constructor is only for testing")]
        public MapControl()
        {
            // Default Values for MapSize
            X = 5;
            Y = 5;
            for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 5; y++)
                {
                    MapCommon newCom = new MapCommon(0, x, y, 0, null);
                    MapCom.Add(newCom);
                } 
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MapControl"/> class.
        /// </summary>
        /// <param name="x">The x size of the map</param>
        /// <param name="y">The y size of the map</param>
        public MapControl(int x, int y, List<MapCommon> com)
        {
            if (x < 1 || y < 1)
            {
                throw new BGFControlException("Initialization values can't be negative or zero");
            }

            if (com.Count == 0)
            {
                throw new BGFControlException("MapCommon list is null");
            }

            X = x;
            Y = y;
        }

        #endregion constructor

        #region controls

        protected List<Image> MyImages = new List<Image>();
        protected Table ImageTable = new Table();

        #endregion controls

        #region events

        /// <summary>
        /// Handles the Click event of the ImageButtons control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        protected void ImageButtons_Click(object sender, ImageClickEventArgs e)
        {
            UImageButton img = (UImageButton)sender;
            MapCommon com = (MapCommon)img.Tag;

        }
      
        #endregion events

        #region props

        /// <summary>
        /// Gets or sets the X.
        /// </summary>
        /// <value>The X.</value>
        private int X { get; set; }

        /// <summary>
        /// Gets or sets the Y.
        /// </summary>
        /// <value>The Y.</value>
        private int Y { get; set; }

        /// <summary>
        /// Gets or sets the map COM.
        /// </summary>
        /// <value>The map COM.</value>
        private List<MapCommon> MapCom = new List<MapCommon>();

        #endregion props

        #region methods

        /// <summary>
        /// Wird vom ASP.NET-Seitenframework aufgerufen, 
        /// um Serversteuerelemente mit kompositionsbasierter Implementierung 
        /// zu benachrichtigen, dass alle enthaltenen untergeordneten Steuerelemente als 
        /// Vorbereitung auf einen Postback oder eine Wiedergabe erstellt werden sollen.
        /// </summary>
        protected override void CreateChildControls()
        {
            for (int a = 0; a < X; a++)
            {
                TableRow row = new TableRow();
                for (int i = 0; i < Y; i++)
                {
                    UImageButton img = new UImageButton();
                    // URL
                    img.ImageUrl = "http://www.dergrauewolf.de/bilder/packeis.png";
                    MapCommon com = new MapCommon();
                    com.X = 10;
                    com.Y = 20;
                    img.Tag = com;
                    img.Click += new ImageClickEventHandler(ImageButtons_Click);


                    TableCell cell = new TableCell();
                    row.Cells.Add(cell);
                    cell.Controls.Add(img);

                }

                ImageTable.Rows.Add(row);
            }

            Controls.Add(ImageTable);
        }

        #endregion methods
    }
}
